﻿var PromotionMgr = {
    queryGoodsNum: 10,
    promotionGoodsInfo: "ss.pro.goods",
    promotionGoodsList: "ss.pro.goods.list",
    promotionGoodsListLeft: "ss.pro.goods.left",
    setPromotionGoodsInfo: function (promotionGoodsInfo) {
        var curPromotionGoodsInfo = PromotionMgr.getPromotionGoodsInfo();
        if (null != curPromotionGoodsInfo) {
            if (curPromotionGoodsInfo.goods_info.goodsid != promotionGoodsInfo.goods_info.goodsid) {
                removeStorage(PromotionMgr.promotionGoodsList);
                removeStorage(PromotionMgr.promotionGoodsListLeft);
                setStorage(PromotionMgr.promotionGoodsInfo, promotionGoodsInfo);
            }
        } else {
            removeStorage(PromotionMgr.promotionGoodsList);
            removeStorage(PromotionMgr.promotionGoodsListLeft);
            setStorage(PromotionMgr.promotionGoodsInfo, promotionGoodsInfo);
        }
    },
    getPromotionGoodsInfo: function () {
        return getStorageVal(PromotionMgr.promotionGoodsInfo);
    },
    resetPromotionGoodsInfo: function () {
        removeStorage(PromotionMgr.promotionGoodsInfo);
        removeStorage(PromotionMgr.promotionGoodsList);
        removeStorage(PromotionMgr.promotionGoodsListLeft);
    },
    resetPromotionGoodsNum: function (goodsId) {
        var goodsList = PromotionMgr.getPromotionGoodsList();
        if (null != goodsList) {
            var bChanged = false;
            for (index in goodsList) {
                if (goodsId == goodsList[index].goods_info.goodsid) {
                    bChanged = true;
                    goodsList[index].goods_num = 0;
                    break;
                }
            }
            if (true == bChanged) {
                setStorage(PromotionMgr.promotionGoodsList, goodsList);
            }
        }
    },
    setPromotionGoodsListLeft: function(leftNum) {
        setStorage(PromotionMgr.promotionGoodsListLeft, leftNum);
    },
    getPromotionGoodsListHasLeft: function() {
        var hasLeft = true;
        var hasLeftVal = getStorageVal(PromotionMgr.promotionGoodsListLeft);
        if ((null != hasLeftVal) && (Number(hasLeftVal) == 0)) {
            hasLeft = false;
        }
        return hasLeft;
    },
    getPromotionGoodsList: function() {
        return getStorageVal(PromotionMgr.promotionGoodsList);
    },
    addpromotionGoodsList: function (goodsList) {
        var promotionGoods = PromotionMgr.getPromotionGoodsInfo();
        var promotionGoodsList = PromotionMgr.getPromotionGoodsList();
        if (null == promotionGoodsList) {
            promotionGoodsList = new Array();
        }
        var bChanged = false;
        for (index in goodsList) {
            var num = ShoppingMgr.getShoppingGoodsNum(goodsList[index].goodsid);
            var goodsInfo = {
                "goods_info": goodsList[index],
                "goods_num": num,
                "main_tab": promotionGoods.main_tab,
                "sub_tab": promotionGoods.sub_tab,
                "out_stack": GoodsMgr.isNotOutStack
            }
            promotionGoodsList.push(goodsInfo);
            bChanged = true;
        }
        if (true == bChanged) {
            setStorage(PromotionMgr.promotionGoodsList, promotionGoodsList);
        }
    },
    getPromotionRequestJson: function (goodsInfo, from, num) {
        var teminalId = TerminalMgr.getCurTerminalId();
        var getRequestJson = {
            "terminalid": teminalId,
            "goodtype": goodsInfo.goods_info.goods_type,
            "goodid": Number(goodsInfo.goods_info.goodsid),
            "from": from,
            "number": num
        };
        return getRequestJson;
    },
    getPromotionGoodsListFromService: function (goodsInfo, from, num, getPromotionGoodsListCallback) {
        var getRequestJson = PromotionMgr.getPromotionRequestJson(goodsInfo, from, num);
        doAjax(getRequestJson, "get_goods_list", function (msg) {
            if (msg.result == 0) {
                var teminalId = TerminalMgr.getCurTerminalId();
                if (Number(msg.terminalid) == teminalId) {
                    PromotionMgr.addpromotionGoodsList(msg.goods_info);
                    if (null != getPromotionGoodsListCallback) {
                        var goodsList = PromotionMgr.getPromotionGoodsList();
                        getPromotionGoodsListCallback(goodsList);
                    }
                }
            }
        });
    },
    tryGetPromotionGoodsListFromService: function (goodsInfo, from, num, getPromotionGoodsListCallback) {
        var hasLeft = PromotionMgr.getPromotionGoodsListHasLeft();
        if (true == hasLeft) {
            PromotionMgr.getPromotionGoodsListFromService(goodsInfo, from, num, getPromotionGoodsListCallback);
        } else {
            var promotionGoodsList = PromotionMgr.getPromotionGoodsList();
            if (null != getPromotionGoodsListCallback) {
                var goodsList = PromotionMgr.getPromotionGoodsList();
                getPromotionGoodsListCallback(goodsList);
            }
        }
    },
    getPromotionGoodsListInfo: function (goodsInfo, from, num, getPromotionGoodsListCallback) {
        var promotionGoodsList = PromotionMgr.getPromotionGoodsList();
        if (null != promotionGoodsList) {
            var size = promotionGoodsList.length;
            if (size > (from + num)) {
                if (getPromotionGoodsListCallback) {
                    getPromotionGoodsListCallback(promotionGoodsList);
                }
            } else {
                PromotionMgr.tryGetPromotionGoodsListFromService(goodsInfo, size, PromotionMgr.queryGoodsNum, getPromotionGoodsListCallback);
            }
        } else {
            PromotionMgr.tryGetPromotionGoodsListFromService(goodsInfo, 0, PromotionMgr.queryGoodsNum, getPromotionGoodsListCallback);
        }
    },
    getGoodsInfo: function (goodsId, typeOp) {
        var goodsInfo = null;
        var goodsList = PromotionMgr.getPromotionGoodsList();
        if (null != goodsList) {
            var bChanged = false;
            for (index in goodsList) {
                if (goodsId == goodsList[index].goods_info.goodsid) {
                    bChanged = true;
                    goodsList[index].goods_num += typeOp;
                    goodsInfo = goodsList[index];
                    break;
                }
            }
            if (true == bChanged) {
                setStorage(PromotionMgr.promotionGoodsList, goodsList);
            }
        }
        return goodsInfo;
    }
};